/*
 * GeoVISTA Center (Penn State, Dept. of Geography)
 * Copyright (c), 2002, GeoVISTA Center
 * Licensed under Simplified BSD License
 * This library is developed and distributed for free usage,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * You can redistribute and/or modify it under the terms of the
 * Simplified BSD License
 *
 *
 *
 *
 *
 *
*
*
* @author: jin Chen
* @date: Nov 29, 2004$
* @version: 1.0
*/
package edu.psu.geovista.app.pcp.toolkits.colorpalette.v2;

import edu.psu.geovista.app.pcp.toolkits.colorpalette.FixedSwatchPane;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Hashtable;

public class ColorPalette extends JPanel{
    public static final Color DEFAULT_COLOR = new Color(192,192,192);
    public static final Dimension SWATCH_SIZE=new Dimension(22,22);
    private String[] nameList;
    Hashtable colortable=new Hashtable(3); //KEY: item name;  value: color
    PropertyChangeListener colorListener=new PropertyChangeListener() {
        public void propertyChange(PropertyChangeEvent evt) {
            Object newValue = evt.getNewValue();
            if (newValue instanceof Color)            {
                Color color = (Color) newValue;
                ColorPalette.this.selectColorL.setBackground(color);
                
                String  target = (String) targetList.getSelectedValue();
                colortable.put(target,color);
                ColorPalette.this.firePropertyChange(target,null,color);
            }

        }
    };


    public ColorPalette() {
        this.initComponents();
        initValues() ;

    }
    public ColorPalette(String[] nameList) {
        this.nameList =nameList;
        initComponents();
        initValues() ;
        //postInit();
    }

    private void initValues() {
        this.targetList.setSelectedIndex(0);
    }
    private FixedSwatchPane createCustomColorPane(){
        Color[][] mycolors= new Color[3][4];
        for (int i=0;i<mycolors.length ;i++){
            for (int j=0;j<mycolors[i].length ;j++){
                mycolors[i][j]=DEFAULT_COLOR;
            }
        }
        FixedSwatchPaneImpl sp=new CustomizeFixedSwatchPaneImpl(SWATCH_SIZE.width ,SWATCH_SIZE.height ,mycolors);
        sp.addPropertyChangeListener(this.colorListener );
        return sp;
    }

    private FixedSwatchPane createDefaultColorPane(){
        int[][] colorValues=new int[][]{
            {255, 255, 255, // first row.
             192, 192, 192,
             255, 0, 0,
             255, 255, 0,
             0, 255, 0,
             0, 255, 255,
             0, 0, 255,
             255, 0, 255},
            {0, 0, 0,  // second row.
             128, 128, 128,
             128, 0, 0,
             128, 128, 0,
             0, 128, 0,
             0, 128, 128,
             0, 0, 128,
             128, 0, 128,},
            {255, 199, 199,  // 3rd row.
             83, 109, 165,
             204,  255,204,
             153, 153, 204,
             204, 204, 214,
             0, 180, 110,
             252, 253, 205,
             212, 133, 213}
        };
        //Color[][] mycolors= SwatchPanel.convert(colorValues);
        FixedSwatchPaneImpl sp=new FixedSwatchPaneImpl(SWATCH_SIZE.width ,SWATCH_SIZE.height,colorValues);
        sp.addPropertyChangeListener(this.colorListener );
        return sp;

    }

    public void setColortable(Hashtable colortable) {
        this.colortable = colortable;
        //make sure color of selected item is displayed
        Object selected=targetList.getSelectedValue();
        Color color = (Color) this.colortable.get(selected);
        this.selectColorL.setBackground(color);

        //since color table change, need broadcast the info outside
        Enumeration enumeration = colortable.keys();
        while (enumeration.hasMoreElements()) {
            Object key = (Object) enumeration.nextElement();
            //String test[]=nameList;
            Arrays.sort(nameList);
            if(Arrays.binarySearch(nameList,key) !=-1){ //name list contain the key
                Color acolor = (Color) colortable.get(key);
                firePropertyChange(String.valueOf(key),null,acolor);
            }
        }

    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        listP = new javax.swing.JPanel();
        targetList = new javax.swing.JList();
        labelP = new javax.swing.JPanel();
        selectColorL = new javax.swing.JLabel();
        defaultColorP = (javax.swing.JPanel) this.createDefaultColorPane() ;
        // jPanel1 = new javax.swing.JPanel();

        customColorP = (javax.swing.JPanel) createCustomColorPane();
        //jPanel2 = new javax.swing.JPanel();


        setLayout(new java.awt.GridBagLayout());


        //listP.setPreferredSize(new java.awt.Dimension(80, 80));
        //listP.setLayout(new java.awt.BorderLayout());
        DefaultListModel model=new DefaultListModel();
        if(nameList==null||nameList.length <=0)
            nameList =new String[] {"item1","item2"};//{ "background", "selection", "axis" };  // {"item1","item2"};//
        for (int i=0;i<nameList.length ;i++){
            model.add(i,nameList[i]);
        }
        targetList.setModel(model) ;
        /*targetList.setModel(new javax.swing.AbstractListModel() {
        public static final String TAGET_AXES = "Axes";
        public static final String TAGET_BACKGROUND = "Background";
        public static final String TAGET_SELECTION = "Selection";
        String[] strings = {TAGET_AXES, TAGET_BACKGROUND,  TAGET_SELECTION };
        public int getSize() { return strings.length; }

        public Object getElementAt(int i) { return strings[i]; }
        });*/
        targetList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                targetListValueChanged(evt);
            }
        });

        JScrollPane jScrollPane1=new JScrollPane();
        jScrollPane1.setPreferredSize(new java.awt.Dimension(80, 90));
        jScrollPane1.setViewportView(targetList);

        listP.add(jScrollPane1);//, java.awt.BorderLayout.CENTER);

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.fill = java.awt.GridBagConstraints.VERTICAL ;
        gridBagConstraints.insets = new java.awt.Insets(6, 0, 0, 0);
        //gridBagConstraints.weightx = 1.0;
        add(listP, gridBagConstraints);

        labelP.setLayout(new java.awt.GridBagLayout());

        selectColorL.setBackground(new java.awt.Color(255, 153, 51));
        selectColorL.setText("");
        selectColorL.setBorder(new javax.swing.border.EtchedBorder());
        selectColorL.setPreferredSize(new java.awt.Dimension(40, 70));
        selectColorL.setOpaque(true);
        labelP.add(selectColorL, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        //gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;

        gridBagConstraints.insets = new java.awt.Insets(15, 6, 0, 6);
        //gridBagConstraints.weightx = 0.5;
        add(labelP, gridBagConstraints);

        defaultColorP.setBorder(new javax.swing.border.TitledBorder("Default"));
//        jPanel1.setLayout(new java.awt.GridLayout());



        //defaultColorP.add(jPanel1);

        gridBagConstraints = new java.awt.GridBagConstraints();
        //gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        //gridBagConstraints.weightx = 1.0;
        add(defaultColorP, gridBagConstraints);

        customColorP.setLayout(new java.awt.GridBagLayout());

        customColorP.setBorder(new javax.swing.border.TitledBorder("Customize"));
        //jPanel2.setLayout(new java.awt.GridLayout(3, 4, 5, 5));



        //customColorP.add(jPanel2, new java.awt.GridBagConstraints());

        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.insets = new java.awt.Insets(0, 6, 0, 0);
        //gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        //gridBagConstraints.weightx = 1.0;
        add(customColorP, gridBagConstraints);

    }

    private void targetListValueChanged(ListSelectionEvent evt) {
        Object source = evt.getSource();
        JList targetlist=(JList) source;
        Object target = targetlist.getSelectedValue();
        Color color=(Color) colortable.get(target);
        selectColorL.setBackground(color);

    }


    // Variables declaration - do not modify
    private javax.swing.JPanel customColorP;
    private javax.swing.JPanel defaultColorP;
    private javax.swing.JLabel selectColorL;

    private javax.swing.JList targetList;
    private javax.swing.JPanel labelP;
    private javax.swing.JPanel listP;

   

    private static Hashtable getColorTable(){
        Hashtable ct=new Hashtable();
        ct.put("item1",Color.BLACK);
        ct.put("item2",Color.yellow );
        return ct;
    }
}
